package com.prs.crm.action.sys;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.sys.MailAddress;
import com.prs.crm.service.sys.MailAddressService;

public class MailAddressAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private MailAddressService mailAddressService;

	private PaginationSupport pagination;

	private MailAddress addr;

	private Integer[] addrIds;

	public void setMailAddressService(MailAddressService mailAddressService) {
		this.mailAddressService = mailAddressService;
	}

	public MailAddressService getMailAddressService() {
		return mailAddressService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setAddr(MailAddress addr) {
		this.addr = addr;
	}

	public MailAddress getAddr() {
		return addr;
	}

	public Integer[] getAddrIds() {
		return addrIds;
	}

	public void setAddrIds(Integer[] addrIds) {
		this.addrIds = addrIds;
	}

	@Action("listMailAddr")
	public String listMailAddr() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MailAddress.class);
		this.setPagination(this.getMailAddressService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editMailAddr")
	public String editMailAddr() {
		return SUCCESS;
	}

	@Action(value = "saveMailAddr", results = {
			@Result(name = "success", type = "redirect", location = "listMailAddr"),
			@Result(name = "error", type = "chain", location = "editMailAddr") })
	public String saveMailAddr() {
		if (addr.getName() == null || "".equals(addr.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (addr.getAddr() == null || "".equals(addr.getAddr().trim())) {
			addActionError("地址不能为空");
			return ERROR;
		}
		if (addr.getSmtpPort() == null) {
			addActionError("smtp端口不能为空");
			return ERROR;
		}
		if (addr.getPopPort() == null) {
			addActionError("pop端口不能为空");
			return ERROR;
		}
		this.getMailAddressService().save(addr);
		return SUCCESS;
	}

	@Action(value = "removeMailAddr", results = { @Result(name = "success", type = "redirect", location = "listMailAddr") })
	public String removeMailAddr() {
		this.getMailAddressService().batchDraft(addrIds);
		return SUCCESS;
	}

}
